package com.xxxcompany.shop.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.xxxcompany.shop.dao.IUserDAO;
import com.xxxcompany.shop.dao.impl.UserDAOImpl;
import com.xxxcompany.shop.entity.User;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class LoginFrame extends JFrame {

	private JPanel contentPane;
	private JTextField usernameField;
	private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginFrame frame = new LoginFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D:");
		lblNewLabel.setBounds(127, 95, 54, 15);
		contentPane.add(lblNewLabel);
		
		usernameField = new JTextField();
		usernameField.setBounds(189, 92, 92, 21);
		contentPane.add(usernameField);
		usernameField.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801:");
		lblNewLabel_1.setBounds(127, 140, 54, 15);
		contentPane.add(lblNewLabel_1);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(189, 138, 92, 18);
		contentPane.add(passwordField);
		
		JLabel lblNewLabel_2 = new JLabel("\u5546\u54C1\u7BA1\u7406\u7CFB\u7EDF\u767B\u5F55");
		lblNewLabel_2.setFont(new Font("", Font.PLAIN, 30));
		lblNewLabel_2.setBounds(96, 10, 274, 75);
		contentPane.add(lblNewLabel_2);
		
		JButton btnNewButton = new JButton("\u767B\u5F55");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				IUserDAO userDAO = new UserDAOImpl();
				
				String username = usernameField.getText();
				String password = new String(passwordField.getPassword());
				
				User user=null;
				try {
					user = userDAO.find(username,password);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				if(user!=null)
				{
					JOptionPane.showMessageDialog(null,"¼ɹ");
					
					MainFrame f = new MainFrame();
					f.setVisible(true);
					
					LoginFrame.this.setVisible(false);
				}
				else
				{
					JOptionPane.showMessageDialog(null,"¼ʧ!");
				}
				
			}
		});
		btnNewButton.setBounds(113, 185, 93, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u6CE8\u518C");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		btnNewButton_1.setBounds(231, 185, 93, 23);
		contentPane.add(btnNewButton_1);
	}
}